package com.kycxika.app.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.kycxika.app.R;
import com.kycxika.app.activity.base.BaseActivity;
import com.kycxika.app.bean.LoginBean;
import com.kycxika.app.dbutils.IntImp.KycXkIApi;
import com.kycxika.app.dbutils.inter.HttpQuestResultListener;

public class LoginActivity extends BaseActivity implements HttpQuestResultListener {

    private View mTop;
    private EditText family_username;
    private EditText family_pwd;
    private KycXkIApi httpApi;

    @Override
    protected void initView() {
        mTop = findViewById(R.id.top);
        setTop(mTop);
        getTitle("登录");
        getVisibility(true,this);

        family_username = (EditText) findViewById(R.id.family_username);
        family_pwd = (EditText) findViewById(R.id.family_pwd);

        httpApi = KycXkIApi.getInstance(this);
        httpApi.setHttpQuestResultListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(family_username.getText().toString())) {
                    if(!TextUtils.isEmpty(family_pwd.getText().toString())){
                        httpApi.getLogin(family_username.getText().toString(),family_pwd.getText().toString());
                    }else {
                        Toast.makeText(LoginActivity.this,"密码不能为空.",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(LoginActivity.this,"用户名不能为空.",Toast.LENGTH_SHORT).show();
                }
            }
        });

        findViewById(R.id.regin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,RegisActivity.class));
            }
        });

        findViewById(R.id.family_regis).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void onError(Object e) {

    }

    @Override
    public void onSuccess(Object object) {
        if(object instanceof LoginBean) {
            LoginBean loginBean = (LoginBean) object;
            LoginBean.DataBean dataBean = loginBean.getData();
            if(dataBean != null) {
                Log.i("TAG","name ----- " + dataBean.getUser_name());
                Log.i("TAG","msg ----- " + loginBean.getMsg());
                Toast.makeText(this,loginBean.getMsg(),Toast.LENGTH_SHORT).show();
                httpApi.saveUerRegistionStatu(true);
                httpApi.saveUsername(dataBean.getUser_name());
                httpApi.saveUserNick(dataBean.getUser_nice());
                httpApi.saveUserAcount(dataBean.getUser_name());
                httpApi.saveUserSex(1);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(new Intent(LoginActivity.this,MainActivity.class));
                        LoginActivity.this.finish();
                    }
                },2000);
            }
        }
    }

    @Override
    public void onQuestErroCode(Object object) {
        /*if(code == -200) {
            Toast.makeText(this,"登录失败.",Toast.LENGTH_SHORT).show();
        }else if(code == 400) {
            Toast.makeText(this,"非法请求.",Toast.LENGTH_SHORT).show();
        }else if(code == -500){
            Toast.makeText(this,"Toekn非法.",Toast.LENGTH_SHORT).show();
        }*/
        if(object instanceof LoginBean) {
            LoginBean loginBean = (LoginBean) object;
            Toast.makeText(this,loginBean.getMsg(),Toast.LENGTH_SHORT).show();
        }
    }
}
